namespace ETestIT.UI.Presentation
{
    using System.IO;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using Domain.Entities;

    public static class UserControlsExtensions
    {
        public static void GetResumeContentFromRequest(this HtmlInputFile htmlInputFile, Resume theResume)
        {

            if (htmlInputFile.PostedFile == null) 
                return;

            var httpPostedFile = htmlInputFile.PostedFile;
            var nFileLen = httpPostedFile.ContentLength;

            if (nFileLen > 0)
            {
                var httpPostedFileByteArray = new byte[nFileLen];
                httpPostedFile.InputStream.Read(httpPostedFileByteArray, 0, nFileLen);
                
                var strFilename = Path.GetFileName(httpPostedFile.FileName);

                theResume.Content = httpPostedFileByteArray;
                theResume.Name = strFilename;
                theResume.ContentType = httpPostedFile.ContentType;
            }
        }

        public static void WriteResumeToResponse(this HttpResponse httpResponse, Resume resume)
        {
            httpResponse.ContentType = resume.ContentType;
            httpResponse.BinaryWrite(resume.Content);
            httpResponse.End();
        }
    }
}